#pragma once
#include <functional>
#include <string>

#include "TcpServer.hpp" 

class Connection; // 这个不写，下面三个函数会报错
class TcpServer;   //牛逼，第一次遇到，为什么要有这个，因为在TcpServer.hpp里它include了connection.hpp
using func_t = std::function<void(Connection *)>;
class Connection
{
public:
    Connection(int sockfd, uint32_t events, TcpServer *conn)
        : _sockfd(sockfd), _events(events), _R(conn)
    {
    }
    void RegistCallBack(func_t recver, func_t sender, func_t excepter)
    {
        _recver = recver;
        _sender = sender;
        _excepter = excepter;
    }
    int GetSockfd()
    {
        return _sockfd;
    }
    uint32_t GetEvents()
    {
        return _events;
    }

private:
    int _sockfd;
    std::string _inbuffer;
    std::string _outbuffer;

    uint32_t _events;

public:
    func_t _recver; // 工厂里面初始化
    func_t _sender;
    func_t _excepter;

    TcpServer *_R;
};

class ConnectionFactory
{
public:
    static Connection *BulidListenConnection(int listensock, func_t recver, uint32_t events, TcpServer *R)
    {
        Connection *conn = new Connection(listensock, events, R);
        conn->RegistCallBack(recver, nullptr, nullptr);
        return conn;
    }
    static Connection *BulidNormalConnection(int sockfd,
                                             func_t recver, func_t sender, func_t excepter,
                                             uint32_t events, TcpServer *R)
    {
        Connection *conn = new Connection(sockfd, events, R);
        conn->RegistCallBack(recver, sender, excepter);
        return conn;
    }
};
